// AWTFocusManager.cpp: implementation of the AWTFocusManager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "AWTTest.h"
#include "AWTFocusManager.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNAMIC(AWTFocusManager, AWTObject)

AWTFocusManager::AWTFocusManager(AWTContainer* cont)
{
	m_focusRoot = cont;
	m_focusOwner = NULL;
}

AWTFocusManager::~AWTFocusManager()
{
}

void AWTFocusManager::activateFocus()
{
	BOOL assigned = FALSE;
	if (m_focusOwner != NULL) 
	{
		if ((assigned = assignFocus(m_focusOwner, FALSE)) != TRUE) 
		{
			assigned = focusNext(m_focusOwner);
		}
	} 
	else 
	{
		// assign to first component capable of taking it
		assigned = focusForward(m_focusRoot);
	}
	if (!assigned) 
	{
		m_focusRoot->requestFocus(); 
	}
}

BOOL AWTFocusManager::focusNext(AWTComponent* base)
{
	AWTComponent* target = base;
	if (target != NULL && target->getParent() != NULL) 
	{
		do {
			BOOL found = FALSE;
			AWTContainer* p = (AWTContainer*)target->getParent();
			AWTComponent* c;
			for (int i = 0; i < p->getComponentCount(); i++) 
			{
				c = p->getComponent(i);
				if (found) 
				{
					if (assignFocus(c)) 
					{
						return TRUE;
					}
					if (c->isContainer() && c->isVisible() && c->isEnabled()) 
					{
						if (focusForward((AWTContainer*)c)) 
						{
							return TRUE;
						}
					} 	    
				} 
				else if (c == target) 
				{
					found = TRUE;	
				}
			} 
			target = p;
		} while (target != m_focusRoot && target->getParent() != NULL);
	}
	// wrap-around
	if (focusForward(m_focusRoot)) 
	{
			return true;
	}

	return false;		
}

BOOL AWTFocusManager::focusPrevious(AWTComponent* base)
{
	AWTComponent* target = base;
	if (target != NULL && target->getParent() != NULL) 
	{
    do {
			BOOL found = FALSE;
			AWTContainer* p = (AWTContainer*)target->getParent();
			AWTComponent* c;
			for (int i = p->getComponentCount()-1; i >= 0; i--) 
			{
				c = p->getComponent(i);
				if (found) 
				{
					if (assignFocus(c)) 
					{
						return TRUE;
					}
					if (c->isContainer() && c->isVisible() && c->isEnabled()) 
					{
						if (focusBackward((AWTContainer*)c)) 
						{
							return TRUE;
						}
					} 	    
				} 
				else if (c == target) 
				{
					found = true;	
				}
			} 
			target = p;
    } while (target != m_focusRoot);
	}
	// wrap-around
	if (focusBackward(m_focusRoot)) 
	{
			return true;
	}

	return false;		
}

BOOL AWTFocusManager::assignFocus(AWTComponent* c, BOOL requireTraversable) 
{
	if (c->isVisible() && c->isEnabled() &&
		 (!requireTraversable || c->isFocusTraversable())) 
	{
		c->requestFocus();
		return true;
	}
	return false;
}

BOOL AWTFocusManager::focusForward(AWTContainer* cont) 
{
	for (int i = 0; i < cont->getComponentCount(); i++) 
	{
		AWTComponent* c = cont->getComponent(i);
		if (assignFocus(c)) 
		{
			return true;
		}
		if (c->isContainer() && c->isVisible() && c->isEnabled()) 
		{
			if (focusForward((AWTContainer*)c)) 
			{
				return true;
			}
		} 
	}
	return false;
}

BOOL AWTFocusManager::focusBackward(AWTContainer* cont) 
{
  for (int i = cont->getComponentCount()-1; i >= 0; i--) 
	{
		AWTComponent* c = cont->getComponent(i);
		if (assignFocus(c)) 
		{
			return true;
		}
    if (c->isContainer() && c->isVisible() && c->isEnabled()) 
		{
			if (focusBackward((AWTContainer*)c)) 
			{
				return true;
		  }
    } 
  }
  return false;
}





